﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using simpleHotel.Business;

namespace simpleHotel.Web
{
    public partial class HotelDetail : System.Web.UI.Page
    {
        HotelBLL obj = new HotelBLL();
        int hotelid = 0;

        protected string hid = string.Empty;
        protected string hname = string.Empty;

        protected string arrDate = string.Empty;
        protected string arrDayOfWeek = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["CHECKINDATE"] != null && Session["CHECKOUTDATE"] != null)
                {
                    DateTime checkin = Convert.ToDateTime(obj.ReplaceDate(Session["CHECKINDATE"] + ""));
                    DateTime checkout = Convert.ToDateTime(obj.ReplaceDate(Session["CHECKOUTDATE"] + ""));
                    TimeSpan res = checkout.Subtract(checkin);

                    double totalday = res.TotalDays + 1;

                    for (int i = 0; i < totalday; i++)
                    {
                        arrDate += string.Format("{0:dd/MM/yyyy}", checkin.AddDays(i)) + ";";
                        arrDayOfWeek += checkin.AddDays(i).DayOfWeek.ToString() + ";";
                    }

                    arrDate = arrDate.Substring(0, arrDate.Length - 1);
                    arrDayOfWeek = arrDayOfWeek.Substring(0, arrDayOfWeek.Length - 1);
                }

                if (Request.Params["ID"] != null)
                {
                    hotelid = Int32.Parse(Request.Params["ID"].ToString());
                    hid = hotelid.ToString();
                    GetHotel(hotelid);

                    if (Session["CHECKINDATE"] != null && Session["CHECKOUTDATE"] != null)
                        GetRoomType(hotelid);
                }
            }
        }

        private void GetHotel(int hotelid)
        {
            Hotel hotel = obj.GetHotelByID(hotelid);
            lblHotelID.Text = hotelid.ToString();
            lblHotelName.Text = hotel.HotelName;
            lblHotelDescription.Text = hotel.Description;
            hname = hotel.HotelName;
            Session.Add("HOTELSELECT", hotel.HotelName);
        }

        private void GetRoomType(int hotelid)
        {
            DataTable tb = new DataTable();
            tb = obj.GetRoomTypeByHotelID(hotelid);
            rpRooms.DataSource = tb;
            rpRooms.DataBind();
        }

        protected bool CheckRoom(int hotelid, int roomtypeid)
        {
            HotelBLL obj = new HotelBLL();
            return obj.CheckRoomAvailable(hotelid, roomtypeid);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            

            ////int SumPrice = 0;
           
            ////foreach (RepeaterItem item in rpRooms.Items)
            ////{
            ////   Label lblRoomID = (Label)item.FindControl("lblRoomID");
            ////   Label lblRoom = (Label)item.FindControl("lblRoomName");
            ////   TextBox txt = (TextBox)item.FindControl("txtNumberOfRoom");
            ////   Label lbl = (Label)item.FindControl("lblprice1");
            ////   double d = double.Parse(lbl.Text);
            ////   string price = String.Format("{0:0}", d);
            ////   int totalpaid=0;

            ////    if (!string.IsNullOrEmpty(txt.Text))
            ////   {
            ////       SumPrice += Int32.Parse(txt.Text) * Int32.Parse(price);
            ////       totalpaid=Int32.Parse(txt.Text) * Int32.Parse(price);
            ////       DataRow row = tb.NewRow();

            ////       row["HotelID"] = lblHotelID.Text;
            ////       row["HotelName"] = lblHotelName.Text;
            ////       row["RoomID"] = lblRoomID.Text;
            ////       row["RoomName"] = lblRoom.Text;
            ////       row["NumberOfRoom"] = txt.Text;
            ////       row["Price"] = d;
            ////       row["TotalPaid"] = totalpaid;
            ////       tb.Rows.Add(row);
            ////   }
            ////}

            ////Session.Add("SUMPRICE",SumPrice.ToString());
            
            ////Session.Add("BOOKINGINFO", tb);

            
        }

        protected void rpRooms_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            TextBox txtNumberRoom = (TextBox)e.Item.FindControl("txtNumberOfRoom");
            Button btn = (Button)e.Item.FindControl("btnCheckIn");
            Label lblRoomName = (Label)e.Item.FindControl("lblRoomName");
            Label lblRoomTypeID = (Label)e.Item.FindControl("lblRoomCategory");
            System.Web.UI.HtmlControls.HtmlInputHidden h = (System.Web.UI.HtmlControls.HtmlInputHidden)e.Item.FindControl("txtHidData");

            DataTable tb = new DataTable();
            tb.Columns.Add("HotelID", typeof(int));
            tb.Columns.Add("HotelName", typeof(string));
            tb.Columns.Add("RoomID", typeof(int));
            tb.Columns.Add("RoomName", typeof(string));
            tb.Columns.Add("NumberOfRoom", typeof(int));
            tb.Columns.Add("Price", typeof(int));
            tb.Columns.Add("TotalPaid", typeof(int));

            DataRow row = tb.NewRow();

            row["HotelID"] = lblHotelID.Text;
            row["HotelName"] = lblHotelName.Text;
            row["RoomID"] = lblRoomTypeID.Text;
            row["RoomName"] = lblRoomName.Text;
            row["NumberOfRoom"] = txtNumberRoom.Text;
            row["Price"] = "0";
            row["TotalPaid"] = h.Value;
            tb.Rows.Add(row);

            Session["TBBOOKING"] = tb;
            Response.Redirect("BookingInfo.aspx");
        }

        protected void btForEnter_Click(object sender, EventArgs e)
        {

        }

    }
}